import TableData from "@/components/public/el-table"; //主页面列表
import TableLyout from '@/components/commonPublic/tableLyoutSpecification.vue'
import tree from "@/components/materialDataManage/specificationManagement/tree"; //左侧机构树
import actions from "@/actions";
import formJs from "./regexDetail.js"; //新增弹框配置
import CustomDialog from "@/components/materialDataManage/specificationManagement/index.vue";
import messageBox from "@/components/public/messageBox"; //主页面分页

import {
    getEntityDetectionList, //列表页接口
    deleteSpecificationDataById, //删除
    getEntityList,
    saveSpecificationData
} from "@/api/request.js";
import publicForm from "@/components/materialDataManage/fromCom.vue"; //弹框form
export default {
    name: "entityDetectionIndicators",
    data() {
        return {
            name: "entityDetectionIndicators",

            userNature: null, //用户Nature
            showMsgBox: false,
            msgTip: "请先选中数据行！",
            msgType: 1,




            treelength: false, //左侧树显示隐藏
            treeData: [], //树状数据
            showName: "未选择种类",
            defaultProps: {
                children: "children",
                label: "structureName",
            },
            checkData: [],
            navigatepageNums: null,
            isLock: false,
            projectId: null,
            projectName: null,
            loading: false,
            btnObj: formJs.btnObj, //按钮
            queryCriteriaData: {}, //分页
            searchObj: formJs.searchObj, //搜索条件
            table_config: formJs.table_config, //列表
            radio: "待办",
            // 列表
            getList: {
                page: 1,
                pageSize: 15,
                fCompanyid: "",
            },
            isShow: false, //是否显示弹框

            statusNum: null, //点击那个按钮
            dataFid: null, //列表详情id
            currentRow: null, //列表详情
            activeIndex: "1",
            fId: null, //当前数据fId
            tableData: [],
            companyId: null,
            currentRowId: "",
            dialogTitle: null,
        };
    },
    components: {
        TableLyout,
        publicForm,
        TableData,
        tree,
        CustomDialog,
        messageBox

    },
    mounted() {
        this.getTree();
        actions.onGlobalStateChange((state, prev) => {
            var data = prev.currentUser.projects;
            this.userInfo = prev.currentUser



        }, true);

    },
    methods: {
        confirmMsg() {
            this.showMsgBox = false;
        },
        confirmText(status) {
            if (status == '删除') {
                this.msgTip = "此操作将永久删除该数据, 是否继续?";
                this.titelName = "删除";
            } else if (status == '复制') {
                this.msgTip = "此操作将复制该数据, 是否继续?";
                this.titelName = "复制";
            }
            this.msgType = 2; // 0 带功能确定  1 无功能确定  2 带功能确定、取消
            this.showMsgBox = true;

        },
        // 带功能确定
        confirmMsgs() {
            this.showMsgBox = false;
            if (this.titelName == "删除") {
                this.yj_delete();
            } else if (this.titelName == "复制") {
                this.yj_copy();
            }
        },
        refreshList() { //刷新列表

            this.$nextTick(() => {
                this.yj_searchForm(this.getList);
            })
        },
        closeDialog() {
            this.isShow = false;
        },
        getTree() {
            // var obj = {
            //     page: 1,
            //     pageSize: 500
            // }
            getEntityList({})
                .then((res) => {
                    if (res.errcode == 0) {
                        this.treeData=res.data;
                        // this.treeData = []
                        // // var data=[]
                        // res.data.pageInfo.list.map(item => {
                        //     if (item.fStatus == 1) {
                        //         item.name = item.fVarietyname
                        //         item.id = item.fVarietyid
                        //         item.materialList.map(i => {
                        //             i.name = i.fMaterialname
                        //             i.id = i.fMaterialid
                        //         })
                        //         this.treeData.push(item)

                        //     }
                        // })

                        // if (this.treeData.length == 0) {
                        //     this.treelength = false; //左侧组织机构是否显示
                        // } else {
                        //     this.treelength = true;
                            this.checkData = [this.treeData[0].fId]
                        //     this.getList.fId = this.treeData[0].id
                        //     this.showName = this.treeData[0].fVarietyname; //当前点击组织机构名称
                        // }
                        this.getList.fCompanyid = this.treeData[0].fId;
                        this.yj_searchForm(this.getList);
                    } else {
                        this.$message.error(res.msg);
                        return false;
                    }
                })
                .catch(() => {
                    return false;
                });
        },
        // 点击左侧机构树
        handleNodeClick(data) {
            this.getList.name = ""
            this.showName = data.name; //顶部显示机构名
            this.getList.fCompanyid= data.fId;
            this.yj_searchForm(this.getList);
        },
        handleRow() {},

        getBtnName(name, status) {
            this.statusNum = status
            this.yj_edit(status)
        },

        // 新增/编辑
        yj_edit(num) {
            this.statusNum = num;
            var data ;
            this.dialogTitle = "材料规格管理"
            if (num == 1) { //材料规格管理新增
                this.$router.push({
                    path: "/materialDataManage/specificationManagementText",
                });
               data = {
                    data: this.currentRow,
                    getList: this.getList,
                    status: num,
                }
               
            }
            if (num == 2) { //材料规格管理复制
                // this.editText();

                if (this.dataFid != null) {
                    this.yj_copy(this.currentRow)
                } else {
                    this.$message.warning({
                        content: '请先选择需要复制的数据',
                        duration: 1000
                    })
                }
            } else if (num == 3) { //  材料规格管理编辑
                if (this.dataFid != null) {
                    this.$router.push({
                        path: "/materialDataManage/specificationManagementText",
                    });
                   data = {
                        data: this.currentRow,
                        getList: this.getList,
                        status: num,
                    }
                } else {
                    this.$message.warning({
                        content: '请先选择需要编辑的数据',
                        duration: 1000
                    })
                }
            } else if (num == 4) { //种类材料删除
                if (this.dataFid != null) {
                    this.yj_delete()
                } else {
                    this.$message.warning({
                        content: '请先选择需要删除的数据',
                        duration: 1000
                    })
                }
            } else if (num == 7) { //种类材料删除
                if (this.dataFid != null) {
                    this.$router.push({
                        path: "/materialDataManage/specificationManagementText",
                    });
                   data = {
                        data: this.currentRow,
                        getList: this.getList,
                        status: num,
                    }
                    this.statusNum = 7
                } else {
                    this.$message.warning({
                        content: '请先选择需要查看的数据',
                        duration: 1000
                    })
                }
            }
            sessionStorage.setItem(
                "GxEmbedded_primaryInfo",
                JSON.stringify(data)
            );
        },
        editText() {
            this.isShow = true;
        },


        // 查看
        // 点击查看
        yj_viewForm(row, label) {
            if (label == "规格型号") {
                this.currentRow = row;
                this.statusNum = 7
                this.$router.push({
                    path: "/materialDataManage/specificationManagementText",
                });
               var data = {
                    data: this.currentRow,
                    getList: this.getList,
                    status: 7,
                    
                }
                sessionStorage.setItem(
                    "GxEmbedded_primaryInfo",
                    JSON.stringify(data)
                );

            }
        },
        // 点击当前行变色
        rowChange(val) {
            this.currentRow = val
            this.dataFid = val.fId;
        },
        // 获取列表数据
        yj_searchForm(data) {
            var obj = {
                page: data.page,
                pageSize: data.pageSize,
                fCompanyid: data.fCompanyid
            }
            this.loading = true;
            var that = this;
            getEntityDetectionList(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        that.loading = false;
                        that.tableData = res.data.pageInfo.list;
                        this.queryCriteriaData = res.data.pageInfo;
                    } else {
                        this.$message.error({
                            content: '列表数据获取失败' + res.msg,
                            duration: 1000
                        })
                        this.loading = false;
                    }
                })
                .catch((error) => {
                    this.$message.error({
                        content: '列表数据获取失败' + error.msg,
                        duration: 1000
                    })
                    this.loading = false;
                });
        },
        // 复制
        yj_copy(data) {
            data.fVersionid = "";
            data.userId = this.userInfo.id;
            data.userName = this.userInfo.userName;
            data['typ'] = "1"
            this.loading = true;
            saveSpecificationData(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = false;
                        this.yj_searchForm(this.getList)
                        this.$message.success({
                            content: "复制成功",
                            duration: 1000
                        });
                    } else {
                        this.loading = false;
                        this.$message.error({
                            content: "复制失败" + res.msg,
                            duration: 1000
                        });
                        console.log("接口报错");
                    }
                })

        },
        // 删除
        yj_delete() {
            if (this.dataFid != "" && this.dataFid != null) {
                this.loading = true;
                this.isLock = true
                deleteSpecificationDataById({
                        fId: this.dataFid,
                    })
                    .then((res) => {
                        if (res.errcode == 0) {
                            this.isLock = false
                            this.loading = false;
                            this.yj_searchForm(this.getList);
                            this.$message.success({
                                content: "删除成功",
                                duration: 1000
                            });
                            this.dataFid = null
                        } else {
                            this.isLock = false;
                            this.$message.error({
                                content: "删除失败" + res.msg,
                                duration: 1000
                            });
                            this.loading = false;
                        }
                    })
                    .catch(() => {
                        console.log("删除失败");
                        this.loading = false;
                    });

            } else {
                this.$message.error({
                    content: "请先选择需要删除的数据",
                    duration: 1000
                });
            }
        },
    },
};